﻿using System;
using Microsoft.Phone.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WindowsPhoneEssentials.Testing;
using WindowsPhoneMVC.Navigation;
using WindowsPhoneMVC.Phone;
using WindowsPhoneMVC.Tests.Navigation.Fakes;

namespace WindowsPhoneMVC.Tests.Phone
{
    [TestClass]
    public class MvcFrameTests
    {
        private MvcFrame frameUnderTest;
        private FakeShell shell;

        [TestInitialize]
        public void Setup()
        {
            frameUnderTest = new MvcFrame();
            shell = new FakeShell(frameUnderTest);
            frameUnderTest.Initialise(shell);
        }

        [TestMethod]
        public void frame_should_disable_content_during_navigation()
        {
            // arrange

            // act
            frameUnderTest.OnNavigationStarted(NavigationDirection.Forward);

            // assert
            Assert.IsFalse(frameUnderTest.IsContentEnabled);
            Assert.IsTrue(frameUnderTest.IsNavigationInProgress);
        }

        [TestMethod]
        public void frame_should_throw_when_show_is_not_called_within_a_navigation()
        {
            AssertEx.Throws<InvalidOperationException>(() => frameUnderTest.Show(new PhoneApplicationPage()));
        }

        [TestMethod]
        public void frame_should_throw_when_navigation_started_is_called_during_navigation()
        {
            frameUnderTest.OnNavigationStarted(NavigationDirection.Forward);
            AssertEx.Throws<InvalidOperationException>(() => frameUnderTest.OnNavigationStarted(NavigationDirection.Forward));
        }

        [TestMethod]
        public void should_restore_on_navigation_failed()
        {
            // arrange
            frameUnderTest.OnNavigationStarted(NavigationDirection.Forward);
            frameUnderTest.Show(new PhoneApplicationPage());
            frameUnderTest.OnNavigationCompleted(true);

            // act
            frameUnderTest.OnNavigationStarted(NavigationDirection.Forward);
            frameUnderTest.OnNavigationCompleted(false);

            // assert
            Assert.IsTrue(frameUnderTest.IsContentEnabled);
        }
    }
}
